//
//  WeatherCarouselView.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/5/10.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "WeatherCarouselView.h"
#import "WeatherView.h"
#import "UIView+Gradient.h"
@interface WeatherCarouselView()

@property (nonatomic, strong)WeatherView * hideView;
@property (nonatomic, strong)WeatherView * currentView;

@property (nonatomic, strong)NSTimer * timer;

@property (nonatomic) NSInteger i;
@end



@implementation WeatherCarouselView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.clipsToBounds = YES;
       // [self addGradientLayer];
        _currentView = [[WeatherView alloc] initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
        //_currentView.backgroundColor = [UIColor orangeColor];
        [self addSubview:_currentView];
        _currentView.backgroundColor = [UIColor clearColor];
        
        _hideView = [[WeatherView alloc] initWithFrame:CGRectMake(0,frame.size.height, frame.size.width, 0)];
       // _hideView.backgroundColor = [UIColor greenColor];
        [self addSubview:_hideView];
       
         [self.timer setFireDate:[NSDate distantPast]];
    }
    return self;
}


- (NSTimer *)timer {
    
    if (!_timer) {
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:4.f target:self selector:@selector(changeView) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
       
    }
    
    return _timer;
}
- (void)dealloc {
    
    [self.timer invalidate];
    self.timer = nil;
}

//- (void)addGradientLayer {
//    
//    UIColor *colorOne = [UIColor colorWithWhite:1 alpha:0.0];
//    UIColor *colorTwo = [UIColor colorWithWhite:1 alpha:0.5];
////    UIColor *colorThree = [UIColor colorWithRed:(216/255.0)  green:(0/255.0)  blue:(18/255.0)  alpha:0.3];
//    
//    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor,nil];
//    CAGradientLayer *gradient = [CAGradientLayer layer];
//    //设置开始和结束位置(设置渐变的方向)
//    gradient.startPoint = CGPointMake(0, 0.5);
//    gradient.endPoint = CGPointMake(1, 0.5);
//    gradient.colors = colors;
//    gradient.frame = self.bounds;
//    [self.layer  addSublayer:gradient];
//}

- (void)setWeatherArray:(NSArray *)weatherArray {
    
    [self.timer setFireDate:[NSDate distantFuture]];
    _weatherArray = weatherArray;
    
    [self.timer setFireDate:[NSDate distantPast]];
}

- (void)changeView {
    
    if (self.weatherArray.count == 0) {
        self.hidden = YES;
        return;
    }
    self.hidden = NO;
    
    NSInteger index = self.i % self.weatherArray.count;
    
    Weather *weather = self.weatherArray[index];
    
    Weather *nextWeather = (index + 1) >= self.weatherArray.count ? self.weatherArray[0] : self.weatherArray[index + 1];
    self.i ++;
    [self.currentView configWeather:weather];
    [self.hideView configWeather:nextWeather];
    
    [UIView animateWithDuration:1 animations:^{
        _hideView.frame = CGRectMake(0,0,self.frame.size.width, self.frame.size.height);
        
        _currentView.frame = CGRectMake(0,-self.frame.size.height,self.frame.size.width, self.frame.size.height );
        
    } completion:^(BOOL finished) {
        _currentView.frame = CGRectMake(0,self.frame.size.height,self.frame.size.width , self.frame.size.height);
        WeatherView * view = _currentView;
        _currentView = _hideView;
        _hideView = view;
        
    }];
}

@end
